package com.liming.service.http.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.http.Method;
import com.liming.service.http.HttpClient;
import com.liming.service.http.enums.TimeOutEnum;
import com.liming.service.http.enums.UrlEnum;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

/**
 * @author liming
 * @date 2025/3/18
 */
@Slf4j
@Service
@AllArgsConstructor
public class HttpClientImpl implements HttpClient {

    public String requestByEnumWithForm(UrlEnum urlEnum, Map<String, Object> formMap) {
        // 根据urlEnum枚举，获取请求类型
        HttpRequest request = initRequest(urlEnum);
        request.form(formMap);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String requestByEnumWithBody(UrlEnum urlEnum, String body) {
        // 根据urlEnum枚举，获取请求类型
        HttpRequest request = HttpUtil.createRequest(urlEnum.getMethod(), urlEnum.getUrl());
        request.body(body);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public HttpRequest initRequest(UrlEnum urlEnum) {
        return HttpUtil.createRequest(urlEnum.getMethod(), urlEnum.getUrl()).timeout(TimeOutEnum.SHORT_TIMEOUT.getTimeout());
    }

    public HttpRequest initRequest(Method method, String url) {
        return HttpUtil.createRequest(method, url).timeout(TimeOutEnum.SHORT_TIMEOUT.getTimeout());
    }

    public String get(String url) {
        HttpRequest request = initRequest(Method.GET, url);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String get(String url, Map<String, Object> query) {
        HttpRequest request = initRequest(Method.GET, url).form(query);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String post(String url, Map<String, Object> query) {
        HttpRequest request = initRequest(Method.POST, url).form(query);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String post(String url, String body) {
        HttpRequest request = initRequest(Method.POST, url).body(body);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    @Override
    public String post(String url, Map<String, Object> query, String body) {
        HttpRequest request = initRequest(Method.POST, url).form(query).body(body);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String put(String url, String body) {
        HttpRequest request = initRequest(Method.PUT, url).body(body);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    @Override
    public String delete(String url, Map<String, Object> query) {
        HttpRequest request = initRequest(Method.DELETE, url).form(query);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String delete(String url, String body) {
        HttpRequest request = initRequest(Method.DELETE, url).body(body);
        try (HttpResponse response = request.execute()) {
            return response.body();
        }
    }

    public String mapToQuery(Map<String, Object> map) {
        return HttpUtil.toParams(map);
    }

    public String mapToQuery(Map<String, Object> map, Charset charset) {
        return HttpUtil.toParams(map, charset);
    }

    public String mapToQuery(Map<String, Object> map, Charset charset, boolean isFormUrlEncoded) {
        return HttpUtil.toParams(map, charset, isFormUrlEncoded);
    }

    public Map<String, String> queryToMap(String paramsStr) {
        return HttpUtil.decodeParamMap(paramsStr, CharsetUtil.CHARSET_UTF_8);
    }

    public Map<String, String> queryToMap(String paramsStr, Charset charset) {
        return HttpUtil.decodeParamMap(paramsStr, charset);
    }

    public Map<String, List<String>> queryToMapList(String paramsStr) {
        return HttpUtil.decodeParams(paramsStr, CharsetUtil.CHARSET_UTF_8);
    }

    public Map<String, List<String>> queryToMapList(String paramsStr, Charset charset) {
        return HttpUtil.decodeParams(paramsStr, charset);
    }

    public Map<String, List<String>> queryToMapList(String paramsStr, Charset charset, boolean isFormUrlEncoded) {
        return HttpUtil.decodeParams(paramsStr, charset, isFormUrlEncoded);
    }

    public boolean isHttp(String url) {
        return HttpUtil.isHttp(url);
    }

    public boolean isHttps(String url) {
        return HttpUtil.isHttps(url);
    }

}
